package com.boylegu.springboot_vue.service.impl;

import com.boylegu.springboot_vue.dao.UsersRepository;
import com.boylegu.springboot_vue.entities.Users;
import com.boylegu.springboot_vue.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UsersRepository userRepository;

    @Override
    public void deleteById(String id) {
        userRepository.delete(id);
    }

    public Users save(Users user) {
        return userRepository.save(user);
    }

    @Override
    public Page<Users> findAll(Pageable page) {
        Page<Users> results = this.userRepository.findAll(page);
        for (Users users : results) {
            String name = users.getName();
        }
        return results;
    }

    @Override
    public List<Users> findAll() {
        return userRepository.findAll();
    }

    @Override
    public Users findById(String id) {
        return userRepository.findById(id);
    }

    public Users findByName(String name) {
        return this.userRepository.findByName(name);
    }

    public int count() throws Exception {
        long size = userRepository.count();
        int count = Integer.valueOf(String.valueOf(size));
        return count;
    }

}
